package com.sjm.suchbear.minibookmall;

import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sjm.suchbear.minibookmall.Utils.SP;
import com.sjm.suchbear.minibookmall.Utils.T;
import com.sjm.suchbear.minibookmall.action.BookManage;
import com.sjm.suchbear.minibookmall.action.CarManage;
import com.sjm.suchbear.minibookmall.action.UserManage;
import com.sjm.suchbear.minibookmall.manage.AddBookActivity;
import com.sjm.suchbear.minibookmall.model.BookModel;
import com.sjm.suchbear.minibookmall.model.CarModel;
import com.sjm.suchbear.minibookmall.model.UserModel;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class BookDetailActivity extends Activity {

    @BindView(R.id.ll_header_back)
    LinearLayout llHeaderBack;
    @BindView(R.id.tv_list_title)
    TextView tvListTitle;
    @BindView(R.id.iv_header_function_ico)
    ImageView ivHeaderFunctionIco;
    @BindView(R.id.ll_header_function)
    LinearLayout llHeaderFunction;
    @BindView(R.id.ll_List_Main)
    RelativeLayout llListMain;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_auth)
    TextView tvAuth;
    @BindView(R.id.tv_content)
    TextView tvContent;
    @BindView(R.id.tv_price)
    TextView tvPrice;
    @BindView(R.id.tv_goCar)
    TextView tvGoCar;

    private SQLiteDatabase dbread;
    private String bookID;
    private BookModel bookModel;
    private UserModel userModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_book_detail);
        ButterKnife.bind(this);
        tvListTitle.setText("书籍详情");
        bookID = getIntent().getStringExtra("bookID");
        BookDB db = new BookDB(getBaseContext());
        dbread = db.getReadableDatabase();
        if (bookID == null) {
            T.showShort(getBaseContext(), "传值错误");
            finish();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        bookModel = BookManage.getBookByID(dbread, bookID);
        userModel = UserManage.findUserByID(dbread, SP.getUid(getBaseContext()));
        tvTitle.setText("书名:".concat(bookModel.getBookname()));
        tvAuth.setText("作者:".concat(bookModel.getBookauth()));
        tvContent.setText("介绍:".concat(bookModel.getBookcontent()));
        tvPrice.setText("￥".concat(bookModel.getBookprice()));

        if (userModel.getAuth() == 1) {
            tvGoCar.setText("删除本书");
            ivHeaderFunctionIco.setImageResource(R.drawable.img_edit);
        }
    }

    @OnClick({R.id.ll_header_back, R.id.tv_goCar, R.id.ll_header_function})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_header_back:
                finish();
                break;
            case R.id.ll_header_function:
                if (userModel.getAuth() == 1) {
                    Intent editIntent = new Intent(BookDetailActivity.this, AddBookActivity.class);
                    editIntent.putExtra("mode", 1);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("bookModel", bookModel);
                    editIntent.putExtras(bundle);
                    startActivity(editIntent);
                }
                break;
            case R.id.tv_goCar:
                if (userModel.getAuth() == 0) {
                    CarModel temp = CarManage.getCarBookbyid(dbread, SP.getUid(getBaseContext()), bookID);
                    if (temp == null) {
                        CarManage.addCar(dbread, SP.getUid(getBaseContext()), bookID, bookModel.getBookname(), bookModel.getBookprice());
                    } else {
                        int num = temp.getNum() + 1;
                        CarManage.changeCarNumbyBook(dbread, SP.getUid(getBaseContext()), bookID, num);
                    }
                    T.showShort(getBaseContext(), "添加成功");
                } else if (userModel.getAuth() == 1) {
                    //清购物车
                    CarManage.deleteCarByBook(dbread, bookID);
                    BookManage.deleteBook(dbread, bookID);
                    T.showShort(getBaseContext(), "删除成功");
                    finish();
                }
                break;
        }
    }
}
